package org.dhbw.cocktail;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import lombok.Data;

/**
 * Named Queries to Prevent SQL-Injection-like vulnerabilities
 */
@NamedQueries({
		@NamedQuery(name = "all_ingredients", query = "SELECT i FROM jc_ingredient i"),
		@NamedQuery(name = "search_ingredient", query = "SELECT i FROM jc_ingredient i WHERE UPPER(i.Name) LIKE :searchTerm ORDER BY i.Name ASC"),
		@NamedQuery(name = "search_ingredient_by_id", query = "SELECT i FROM jc_ingredient i WHERE i.id=:searchTerm") })
		
/**
 * An Instance of this Class is an Ingredient, that can be used for a cocktail
 * Setter/Getter/Constructors are automatically created via annotations by the lombok-Project in the compiler-classpath
 * 
 * @author Roland von Werden
 * @version 0.1
 */
@Data
@Entity(name = "jc_ingredient")
public class Ingredient {

	/** ID for java.persistence */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private long Id;

	/** Name of the Ingredient */
	@Column(name = "name")
	private String Name;

	/** Available Quantity of the Ingredient */
	@Column(name = "amount")
	private double Amount;

	/** Unit of measurement for the Quantity */
	@Column(name = "unit")
	private String Unit;

	public Ingredient(String name, double amount, String unit) {
		this.Name = name;
		this.Amount = amount;
		this.Unit = unit;
	}

	public Ingredient() {
	}

	/**
	 * Removes a defined amount of the ingredient
	 * 
	 * @param amount
	 *            Amount to remove
	 * @return if the removal succeeded
	 */
	public boolean removeAmount(double amount) {

		if (this.Amount - amount < 0)
			return false;
		this.Amount -= amount;
		return true;
	}
}